use crate::git::commands::commit::CommitInfo;
use std::time::{Duration, Instant};
// 应用数据结构
pub struct AppData {
    pub branches: Vec<String>,
    pub commits: Vec<CommitInfo>,
    pub selected_index: usize,
    pub current_branch: Option<String>, // 添加当前分支字段
    pub has_changes: bool,
    pub selected_commits: Vec<usize>, // 新增：用于存储多选的提交索引
    pub merge_source_branch: Option<String>, // 新增：要合并的源分支

    // 通知系统
    pub notification: Option<String>,
    pub notification_time: Option<Instant>,
    pub notification_duration: Option<Duration>,

    // UI 状态
    pub ui_state: AppState,

    // 确认对话框
    pub dialog: Option<ConfirmDialog>,
    pub current_diff: Option<String>, // 新增：当前选中提交的diff信息
}

// UI 状态枚举
#[derive(Debug, Clone, PartialEq)]
pub enum AppState {
    BranchList,
    CommitList,
    MergeList,
    Dialog,
    // 其他状态...
}

// 确认对话框
#[derive(Debug, Clone)]
pub struct ConfirmDialog {
    pub title: String,
    pub message: String,
    pub options: Vec<DialogOption>,
    pub selected_option: usize,
}

// 对话框选项
#[derive(Debug, Clone)]
pub struct DialogOption {
    pub label: String,
    pub value: String,
}

impl AppData {
    pub fn new() -> Self {
        Self {
            branches: Vec::new(),
            commits: Vec::new(),
            selected_index: 0,
            current_branch: None,
            has_changes: false,
            selected_commits: Vec::new(), // 初始化为空
            merge_source_branch: None,
            notification: None,
            notification_time: None,
            notification_duration: None,
            ui_state: AppState::BranchList,
            dialog: None,
            current_diff: None, // 初始化diff信息
        }
    }

    // 切换提交选中状态
    pub fn toggle_commit_selection(&mut self) {
        if self.commits.is_empty() || self.selected_index >= self.commits.len() {
            return;
        }

        if self.selected_commits.contains(&self.selected_index) {
            // 如果已经选中，则取消选中
            self.selected_commits.retain(|&i| i != self.selected_index);
        } else {
            // 如果未选中，则添加到选中列表
            self.selected_commits.push(self.selected_index);
        }
    }

    // 清除所有选中的提交
    pub fn clear_commit_selections(&mut self) {
        self.selected_commits.clear();
    }

    // 是否已选中当前提交
    pub fn is_commit_selected(&self, index: usize) -> bool {
        self.selected_commits.contains(&index)
    }

    // 获取选中的提交ID列表
    pub fn get_selected_commit_ids(&self) -> Vec<String> {
        self.selected_commits
            .iter()
            .filter_map(|&index| {
                if index < self.commits.len() {
                    Some(self.commits[index].id.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    // 设置通知，带有过期时间
    pub fn set_notification(&mut self, message: String, duration_ms: u64) {
        self.notification = Some(message);
        self.notification_time = Some(Instant::now());
        self.notification_duration = Some(Duration::from_millis(duration_ms));
    }

    // 检查通知是否应该被清除
    pub fn check_notification(&mut self) {
        if let (Some(time), Some(duration)) = (self.notification_time, self.notification_duration) {
            if time.elapsed() > duration {
                self.notification = None;
                self.notification_time = None;
                self.notification_duration = None;
            }
        }
    }

    // 显示确认对话框
    pub fn show_dialog(&mut self, title: String, message: String, options: Vec<DialogOption>) {
        self.dialog = Some(ConfirmDialog {
            title,
            message,
            options,
            selected_option: 0,
        });
        self.ui_state = AppState::Dialog;
    }

    // 关闭对话框
    pub fn close_dialog(&mut self) {
        self.dialog = None;
        self.ui_state = AppState::BranchList; // 恢复到之前的状态
    }

    // 选择下一个对话框选项
    pub fn next_dialog_option(&mut self) {
        if let Some(dialog) = &mut self.dialog {
            dialog.selected_option = (dialog.selected_option + 1) % dialog.options.len();
        }
    }

    // 选择上一个对话框选项
    pub fn prev_dialog_option(&mut self) {
        if let Some(dialog) = &mut self.dialog {
            if dialog.selected_option > 0 {
                dialog.selected_option -= 1;
            } else {
                dialog.selected_option = dialog.options.len() - 1;
            }
        }
    }

    // 获取当前选中的对话框选项
    pub fn get_selected_dialog_option(&self) -> Option<DialogOption> {
        self.dialog
            .as_ref()
            .and_then(|dialog| dialog.options.get(dialog.selected_option).cloned())
    }
    // 设置当前分支
    pub fn set_current_branch(&mut self, branch_name: String) {
        self.current_branch = Some(branch_name);
    }

    // 设置修改状态
    pub fn set_has_changes(&mut self, has_changes: bool) {
        self.has_changes = has_changes;
    }
}
